package nl.unimaas.micc.umsnooker.gameflow.phases;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme.math.Vector3f;

import nl.unimaas.micc.umsnooker.domain.BilliardBall;
import nl.unimaas.micc.umsnooker.domain.Table;
import nl.unimaas.micc.umsnooker.gameflow.GameFlow;
import nl.unimaas.micc.umsnooker.gameflow.GamePhase;
import nl.unimaas.micc.umsnooker.gameflow.GameState;

public class FrameInitPhase extends GamePhase
{
	private static final Logger logger = Logger.getLogger(FrameInitPhase.class.getName());

	public FrameInitPhase(GameState state)
	{
		super(state);
	}

	@Override
	public void start()
	{
		super.start();
		
		setDefaults();		

		finish();
	}
	
	private void setDefaults()
	{
		Table table = state.getTable();
		
		// --> Initialize the cue ball
		Vector3f cueBallPosition = new Vector3f(
				table.getBrownSpot().x + table.getDRadius() / 2f,
				table.getBrownSpot().y - table.getDRadius() / 2f,
				0);
		BilliardBall cueBall = new BilliardBall(0, BilliardBall.Type.CUE, cueBallPosition);
		setCueBall(cueBall);
		// We store all balls in a List:
		Set<BilliardBall> balls = new HashSet<BilliardBall>(25);
		balls.add(cueBall);
		
		// --> Initialize the colored balls
		balls.add(new BilliardBall(2, BilliardBall.Type.YELLOW, table.getYellowSpot()));
		balls.add(new BilliardBall(3, BilliardBall.Type.GREEN, table.getGreenSpot()));
		balls.add(new BilliardBall(4, BilliardBall.Type.BROWN, table.getBrownSpot()));
		balls.add(new BilliardBall(5, BilliardBall.Type.BLUE, table.getBlueSpot()));
		balls.add(new BilliardBall(6, BilliardBall.Type.PINK, table.getPinkSpot()));
		balls.add(new BilliardBall(7, BilliardBall.Type.BLACK, table.getBlackSpot()));
		
		// --> Initialize the red balls
		int id = 8;
		for (Vector3f redSpot : table.getRedSpots())
		{
			balls.add(new BilliardBall(id++, BilliardBall.Type.RED, redSpot));
		}
		
		// --> Configure all balls
		for (BilliardBall ball : balls)
		{
			ball.setRadius(table.getBallRadius());
			ball.setMass(.120f);
			ball.setFriction(0.06f);
			ball.setTime(0);
		}
		
		// --> Update GameState with balls
		setBalls(balls);

	}
	
	
	public void setCueBall(BilliardBall ball)
	{
		state.setCueBall(ball);
	}
	
	public void setBalls(Set<BilliardBall> balls)
	{
		state.setBalls(balls);
	}
		

	
	@Override
	public void finish()
	{
		if(finished)
		{
			return;
		}
		for (BilliardBall initial : state.getBalls())
		{
			logger.log(Level.FINE, "initialized ball",initial);
		}
		setNext(new BreakInitPhase(state));
		super.finish();
	}
}
